package com.dayuanit.controller;

import com.dayuanit.controller.request.LoginParam;
import com.dayuanit.controller.request.RegisterApplyUserParam;
import com.dayuanit.entity.ApplyUser;
import com.dayuanit.exception.BusinessEnum;
import com.dayuanit.exception.BusinessException;
import com.dayuanit.response.ExResponse;
import com.dayuanit.service.ApplyUserService;
import com.dayuanit.util.PhoneFormatCheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = "/applyUser")
public class ApplyUserController {

    @Autowired
    private ApplyUserService applyUserService;

    /**
     * 注册前台用户
     *
     * @return
     */
    @RequestMapping(value = "/registerApplyUser", method = RequestMethod.POST)
    @ResponseBody
    public ExResponse registerApplyUser(@RequestBody RegisterApplyUserParam param) {

        try {
            //入参校验
            if (param.getApplyUserName() == null || "".equals(param.getApplyUserName())) {
                throw new BusinessException(BusinessEnum.USERNAME_CAN_NOT_BE_EMPTY);
            }
            if (param.getPhone() == null || "".equals(param.getPhone())) {
                throw new BusinessException(BusinessEnum.PHONE_CAN_NOT_BE_EMPTY);
            }
            //调用注册功能
            applyUserService.register(param);

        } catch (BusinessException e) {
            return ExResponse.buildFailWithoutInfo(e.getExCode(), e.getExMsg());
        } catch (Exception e) {
            e.printStackTrace();
            return ExResponse.buildSystemError();
        }
        return ExResponse.buildSuccessWithoutInfo();

    }

    /**
     * 登录功能
     * @param request
     * @param response
     * @param loginParam
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public ExResponse login(HttpServletRequest request,HttpServletResponse response, @RequestBody LoginParam loginParam) {



        try {
            if(loginParam.getPhone() == null || "".equals(loginParam.getPhone())) {
                throw new BusinessException(BusinessEnum.PHONE_CAN_NOT_BE_EMPTY);
            }
            if(loginParam.getPassword() == null || "".equals(loginParam.getPassword())) {
                throw new BusinessException(BusinessEnum.PASSWORD_CAN_NOT_BE_EMPTY);
            }
            if(!PhoneFormatCheckUtil.isChinaPhoneLegal(loginParam.getPhone())) {
                throw new BusinessException(BusinessEnum.PHONE_FORMART_ERROR);
            }
            ApplyUser applyUser = applyUserService.login(loginParam);
            HttpSession session = request.getSession();
            session.setAttribute("applyUser",applyUser);
            return ExResponse.buildSuccessWithoutInfo();
        }catch (BusinessException e) {
            return ExResponse.buildFailWithoutInfo(e.getExCode(),e.getExMsg());
        }catch (Exception e) {
            e.printStackTrace();
            return ExResponse.buildSystemError();
        }

    }

    /**
     * 校验是否登录
     * @param request
     * @return
     */
    @RequestMapping(value = "checkLogin", method = RequestMethod.GET)
    @ResponseBody
    public ExResponse checkLogin(HttpServletRequest request) {
        HttpSession session = request.getSession();
        ApplyUser applyUser = (ApplyUser)session.getAttribute("applyUser");
        if(applyUser == null) {
            return ExResponse.buildFailWithoutInfo(2,"未登录");
        } {
           return ExResponse.buildSuccessWithoutInfo();
        }
    }


    /**
     * 退出登录
     * @param request
     * @return
     */
    @RequestMapping(value = "loginOut", method = RequestMethod.POST)
    @ResponseBody
    public ExResponse loginOut(HttpServletRequest request) {
        try {
            HttpSession session = request.getSession();
            session.removeAttribute("applyUser");
            return ExResponse.buildSuccessWithoutInfo();
        }catch (Exception e) {
            e.printStackTrace();
            return ExResponse.buildSystemError();
        }
    }

    @RequestMapping(value = "getUserInfo", method = RequestMethod.GET)
    @ResponseBody
    public ExResponse getUserInfo(HttpServletRequest request) {
        HttpSession session = request.getSession();
        ApplyUser applyUser = (ApplyUser)session.getAttribute("applyUser");
        return ExResponse.buildSuccess(applyUser);
    }
}
